package com.lgc.webtest.test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.TimeoutException;

public class Recv {
    //队列名称
    private final static String QUEUE_NAME = "abc";

    public static void main(String[] argv) throws java.io.IOException,
            java.lang.InterruptedException, TimeoutException {
        //打开连接和创建频道，与发送端一样
        ConnectionFactory factory = new ConnectionFactory();
        //设置MabbitMQ所在主机ip或者主机名
        factory.setHost("116.62.132.116");
     /*
     看ConnectionFactory源码：发现host的默认值为localhost或127.0.0.1
     用户名是guest，密码也是guest，和RabbitMQ服务器的用户名和密码是一一对应的，此时，不用明显的设置
     端口也是采用默认的
     */
        factory.setUsername("supplyAdmin");
        factory.setPassword("rose123");
        factory.setPort(25672);
        factory.setVirtualHost("com.ofashion.supply");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明队列，主要为了防止消息接收者先运行此程序，队列还不存在时创建队列。
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println("Waiting for messages.");

        /*//创建队列消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        //指定消费队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
        while (true)
        {
            //nextDelivery是一个阻塞方法（内部实现其实是阻塞队列的take方法）
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println("Received '" + message + "'");
        }*/

    }
}
